package cc.facesign.acl.listener;

import cc.facesign.acl.bean.AclRole;
import cc.facesign.acl.bean.AclUserRole;
import cc.facesign.acl.service.AclMenuService;
import cc.facesign.acl.service.AclRoleMenuService;
import cc.facesign.acl.service.AclRoleService;
import cc.facesign.acl.service.AclUserRoleService;
import cc.facesign.common.type.MqKey;
import cc.facesign.common.utils.bean.MqMessage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

@Component
@Slf4j
@RabbitListener(queues = "map")
public class MqListener {

    @Autowired
    private AclRoleService roleService;

    @Autowired
    private AclMenuService menuService;

    @Autowired
    private AclUserRoleService userRoleService;

    @Autowired
    private AclRoleMenuService roleMenuService;

    @RabbitHandler
    public void solveMap(MqMessage mes) {
        log.info("*** {}" , mes);

        Map<String, Object> map = mes.getData();

        String key = map.get("key").toString();

        // 初始化单位的角色
        if (key.equals(MqKey.MAP_INTI_ROLE)) {
            initRole(map);
        }

        // 分配acl基本权限
        if (key.equals(MqKey.MAP_SET_MANAGE)) {
            setManage(map);
        }

        // 删除acl基本权限
        if (key.equals(MqKey.MAP_DELETE_MANAGE)) {
            deleteManage(map);
        }
    }

    private void deleteManage(Map<String, Object> map) {
        String userId = map.get("userId").toString();
        String type = map.get("type").toString();
        String rid = map.get("rid").toString();

        // 获取roleId
        QueryWrapper<AclRole> w = new QueryWrapper<>();
        w.eq("rid", rid);
        w.eq("roke_key", type);
        AclRole role = roleService.getOne(w);

        if (role != null) {
            String roleId = role.getId();
            log.info("*** delete {}", role);
            // 删除角色、用户关联
            userRoleService.deleteRelate(userId, roleId);
        }
    }

    private void setManage(Map<String, Object> map) {
        String rid = map.get("rid").toString();
        String userId = map.get("userId").toString();
        String type = map.get("type").toString();

        // 获取相关role
        QueryWrapper<AclRole> w = new QueryWrapper<>();
        w.eq("rid", rid);
        w.eq("roke_key", type);
        AclRole role = roleService.getOne(w);

        if (role != null) {
            // 用户是否已关联该角色
            if (!roleService.isRelate(userId, type)) {
                // 用户、role关联
                AclUserRole userRole = new AclUserRole(userId, role.getId());
                userRoleService.save(userRole);
            }
        }
    }

    private void initRole(Map<String, Object> map) {
        String rid = map.get("rid").toString();

        AclRole role1 = new AclRole("单位管理员", "root", "管理自己的单位", rid);
        AclRole role2 = new AclRole("部门管理员", "dept", "拥有部门权限", rid);
        AclRole role3 = new AclRole("会议管理员", "conf", "拥有会议管理权限", rid);

        roleService.saveBatch(Arrays.asList(role1, role2, role3));

        // 配置菜单
        roleService.addMenuList(role1.getId(), Collections.singletonList("1"));
        roleService.addMenuList(role2.getId(), Collections.singletonList("2"));
        roleService.addMenuList(role3.getId(), Collections.singletonList("3"));
    }
}
